package cn.xz.kjg.util

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.xz.kjg.R
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 动态添加集合
 * */
object GetListUtils {

    private val mRVList: ArrayList<RecyclerView> = arrayListOf()
    private val mAdapterList: ArrayList<QnZtListAdapter> = arrayListOf()
    private var mParentView: ConstraintLayout? = null

    fun find(view: ConstraintLayout, //父控件
             context: Context, //上下文
             gradle: ArrayList<GetListData> //几级
    ){
        clearList()
        mParentView = view
        for (i in 0 until gradle.size){
            val mGradle = gradle[i]
            val mRecyclerView = RecyclerView(context)
            mRecyclerView.id = View.generateViewId()
            mRVList.add(mRecyclerView)
            val layoutParams = ConstraintLayout.LayoutParams(
                mGradle.width,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            if (i == 0){
                layoutParams.startToStart = view.id
                layoutParams.topToTop = view.id

                if (mGradle.leftMargin != 0){
                    layoutParams.leftMargin = mGradle.leftMargin
                }
            }else{
                layoutParams.startToEnd = mRVList[i-1].id
                layoutParams.topToTop = mRVList[i-1].id

                if (mGradle.leftMargin != 0){
                    layoutParams.leftMargin = mGradle.leftMargin
                }
            }
            //背景图优先级高
            if (mGradle.bDrawable){
                mRecyclerView.background = AppHelper.getDrawable(R.mipmap.icon_zt_bg)
            }else{
                if (mGradle.bGround.isNotEmpty()){
                    mRecyclerView.setBackgroundColor(Color.parseColor(mGradle.bGround))
                }
            }
            mRecyclerView.layoutParams = layoutParams
            mRecyclerView.layoutManager = LinearLayoutManager(context)
            mRecyclerView.itemAnimator = DefaultItemAnimator()

            val mAdapter = QnZtListAdapter(R.layout.item_get_list,mGradle)
            mRecyclerView.adapter = mAdapter
            mAdapterList.add(mAdapter)
            view.addView(mRecyclerView)
        }
    }

    //设置数据
    fun setListData(i: Int, l: ArrayList<TestData>,k: OnListener){
        val a = getAdapter(i)
        if (a != null){
            a.setList(l)
            a.setOnItemClickListener { adapter, view, position ->
                //点击当前层级，隐藏下一层级
                for (j in 0 until mRVList.size){
                    //这里减一是因为点击的时候是i点击的当前级数，要从下一级开始
                    if (j > i-1){
                        mRVList[j].visibility = View.GONE
                    }
                }
                //这里加一是因为i是当前级数，要从下一级开始
                //下标从零开始为什么还要加一呢？因为获取列表里面减一了
                if (getList(i+1) != null){
                    getList(i+1)!!.visibility = View.VISIBLE
                }
                val mData = adapter.data as ArrayList<TestData>
                //回调
                k.onClick(mData[position])
                //更新选中状态
                mData.forEach {
                    it.select = false
                }
                mData[position].select = true
                adapter.notifyDataSetChanged()
            }
        }
    }

    //获取对应层级的列表
    private fun getList(i: Int): RecyclerView?{
        var mL: RecyclerView? = null
        if (mRVList.isNotEmpty() && i-1 < mRVList.size){
            try {
                mL = mRVList[i - 1]
            }catch (e: java.lang.Exception){
                e.printStackTrace()
            }
        }
        return mL
    }

    //获取对应层级的适配器
    private fun getAdapter(i: Int): QnZtListAdapter?{
        var mAdapter: QnZtListAdapter? = null
        if (mAdapterList.isNotEmpty() && i-1 < mAdapterList.size){
            try {
                mAdapter = mAdapterList[i - 1]
            }catch (e: java.lang.Exception){
                e.printStackTrace()
            }
        }
        return mAdapter
    }

    //置空操作
    private fun clearList(){
        mRVList.clear()
        mAdapterList.clear()
        if (mParentView != null){
            mParentView!!.removeAllViews()
        }
    }

    interface OnListener {
        fun onClick(l: TestData)
    }

    //需要的数据类
    data class GetListData(
        var width: Int = ViewGroup.LayoutParams.WRAP_CONTENT, //宽度
        var leftMargin: Int = 0, //左间距
        var bGround: String = "", //背景颜色
        var bDrawable: Boolean = true, //是否需要背景图片
        var itemSize: Float = 0f, //条目字体大小
        var itemHeight: Int = ViewGroup.LayoutParams.WRAP_CONTENT //条目字体高度
    ):java.io.Serializable

    //模拟数据
    data class TestData(
        var Name: String? = "", //名称
        var Tag: Int? = 0, //分类
        var ParentTag: Int? = 0, //父类
        var select: Boolean = false //是否选中
    ):java.io.Serializable


    class QnZtListAdapter : BaseQuickAdapter<TestData, BaseViewHolder> {

        private var gradle: GetListUtils.GetListData? = null

        constructor(layoutResId: Int, data: GetListUtils.GetListData) : super(layoutResId) {
            this.gradle = data
        }

        override fun convert(helper: BaseViewHolder, item: GetListUtils.TestData) {
            //总布局
            val clGetList = helper.getView<ConstraintLayout>(R.id.cl_get_list)
            //第一条不显示分割线
            if (helper.adapterPosition == 0){
                helper.setGone(R.id.view_line,true)
            }

            val tvGetTxt = helper.getView<TextView>(R.id.tv_get_txt)
            tvGetTxt.text = item.Name

            //选中资源
            val selectRes: Drawable? = AppHelper.getDrawable(R.mipmap.icon_zt_yes)
            var mDrawable: Drawable? = null
            if (item.select) {
                tvGetTxt.setTextColor(Color.parseColor("#FFFFFF"))
                mDrawable = selectRes
            } else {
                tvGetTxt.setTextColor(Color.parseColor("#00E7F7"))
            }
            clGetList.background = mDrawable

            if (gradle != null){
                //界面高度
                if (gradle!!.itemHeight != 0){
                    val lp: ViewGroup.LayoutParams = clGetList.layoutParams
                    lp.height = gradle!!.itemHeight
                    clGetList.layoutParams = lp
                }
                //字体大小
                if (gradle!!.itemSize != 0f){
                    tvGetTxt.textSize = gradle!!.itemSize
                }

            }
        }

    }
}